import java.util.ArrayList;
import java.util.Arrays;

/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2023-01-12 20:37
 * @Version 1.0
 **/
public class testMuliPack2 {
    //多重背包：
    //一共有N中物品，一种物品有M件，
    //方式一:将每种物品展开只剩下一件，就成了01背包。
    public static void testMultipack1() {
        ArrayList<Integer> weight = new ArrayList<>(Arrays.asList(1, 3, 4));
        ArrayList<Integer> value = new ArrayList<>(Arrays.asList(15, 20, 30));
        ArrayList<Integer> num = new ArrayList<>(Arrays.asList(2, 3, 2));
        int bagSize = 10;
        for (int i = 0; i < num.size(); i++) {
            while (num.get(i) > 1) {   //将物品展开为 i
                weight.add(weight.get(i));
                value.add(value.get(i));
                num.set(i, num.get(i) - 1);
            }
        }

        int[] dp = new int[bagSize + 1];
        for (int i = 0; i < weight.size(); i++) { // 遍历物品
            for (int j = bagSize; j >= weight.get(i); j--) {
                dp[j] = Math.max(dp[j], dp[j - weight.get(i)] + value.get(i));
            }
        }
    }

    //方式二： 改变遍历个数
    public static void testMuliPack2() {
        int[] weight = {1, 3, 4};
        int[] velue = {15, 20, 30};
        int[] num = {2, 3, 2};
        int bagSize = 10;
        int[] dp = new int[bagSize + 1];
        for (int i = 0; i < weight.length; i++) {//遍历物品，
            for (int j = bagSize; j >= weight[i]; j--) {//遍历背包，倒序
                //以上是01背包，然后加上一个遍历数组
                for (int k = 1; k <= num[i] && (j - k * weight[i]) >= 0; k++) {
                    dp[j] = Math.max(dp[j], dp[j - k * weight[i]] + k * velue[i]);
                }
            }
        }
    }
}
